home *** CD-ROM | disk | FTP | other *** search
/ Graphics & Sound Program…ng Techniques for the Mac / Graphics and Sound Programming Techniques for the Mac.iso / M&T Graphics & Sound Examples / Metrowerks Versions / C04 Speech / P06 Change Voice / ChangeVoice.c next >
Encoding:
C/C++ Source or Header  |  1995-08-05  |  6.3 KB  |  230 lines  |  [TEXT/MMCC]

  1. //____________________________________________________________
  2. //    ChangeVoice.c
  3. //
  4. //    Copyright © Dan Parks Sydow, 1995
  5. //    From the book:
  6. //    "Graphics and Sound Programming Techniques for the Mac",
  7. //    M&T Books, 1995
  8.  
  9.  
  10. //____________________________________________________________
  11.  
  12. #include <Gestalt.h>
  13. #include <Speech.h>
  14.  
  15.  
  16. //____________________________________________________________
  17.  
  18. void           InitializeToolbox( void );
  19. Boolean        IsSpeechAvailable( void );
  20. SpeechChannel  OpenOneSpeechChannelVoice( VoiceSpec );
  21. void           OpenSpeechDialog( void );
  22. OSErr          GetVoiceSpecBasedOnAgeGender( VoiceSpec *, short, short, short );
  23.  
  24.  
  25. //____________________________________________________________
  26.  
  27. #define         rSpeechDialog                     128
  28. #define         kPlaySpeechButton                   1
  29. #define         kSetSpeechYoungBoyButton            2
  30. #define         kSetSpeechMiddleAgeWomanButton      3
  31. #define         kSetSpeechRobotButton               4
  32. #define         kSetSpeechDefaultButton             5
  33. #define         kQuitButton                         6
  34. #define         kNoMatchingVoiceErr              -999
  35.  
  36. //____________________________________________________________
  37.  
  38. void  main( void )
  39.    Boolean  speechPresent;
  40.    
  41.    InitializeToolbox();
  42.  
  43.    speechPresent = IsSpeechAvailable();
  44.    if ( speechPresent == false )
  45.       ExitToShell();
  46.  
  47.    OpenSpeechDialog();
  48. }
  49.  
  50.  
  51. //____________________________________________________________
  52.  
  53. void  OpenSpeechDialog( void )
  54. {
  55.    DialogPtr         theDialog;
  56.    short             theItem;
  57.    Boolean           allDone = false;
  58.    OSErr             theError;
  59.    SpeechChannel     theChannel;
  60.    Str255            theString = "\pMilwaukee, that's in Minnesota isn't it?";
  61.    short             theAgeLo;
  62.    short             theAgeHi;
  63.    short             theGender;
  64.    VoiceSpec         theDefaultVoiceSpec;
  65.    VoiceSpec         theVoiceSpec;
  66.    VoiceDescription  theVoiceDesc;
  67.    
  68.    theDialog = GetNewDialog( rSpeechDialog, nil, (WindowPtr)-1L );
  69.    ShowWindow( theDialog );
  70.    SetPort( theDialog );
  71.    
  72.    theError = GetVoiceDescription( nil, &theVoiceDesc, sizeof( theVoiceDesc ) ); 
  73.    
  74.    theDefaultVoiceSpec = theVoiceDesc.voice;
  75.    theVoiceSpec = theDefaultVoiceSpec;
  76.    
  77.    while ( allDone == false )
  78.    {
  79.       ModalDialog( nil, &theItem );
  80.              
  81.       switch ( theItem )
  82.       {
  83.          case kSetSpeechYoungBoyButton:
  84.             theGender = kMale;
  85.             theAgeLo  =  5;
  86.             theAgeHi  = 10;
  87.             theError = GetVoiceSpecBasedOnAgeGender( &theVoiceSpec, theAgeLo, 
  88.                                                      theAgeHi, theGender );
  89.             break;
  90.  
  91.          case kSetSpeechMiddleAgeWomanButton:
  92.             theGender = kFemale;
  93.             theAgeLo  = 30;
  94.             theAgeHi  = 50;
  95.             theError = GetVoiceSpecBasedOnAgeGender( &theVoiceSpec, theAgeLo, 
  96.                                                      theAgeHi, theGender );
  97.             break;
  98.  
  99.          case kSetSpeechRobotButton:
  100.             theGender = kNeuter;
  101.             theAgeLo  =   500;
  102.             theAgeHi  = 10000;
  103.             theError = GetVoiceSpecBasedOnAgeGender( &theVoiceSpec, theAgeLo, 
  104.                                                      theAgeHi, theGender );
  105.             break;
  106.  
  107.          case kSetSpeechDefaultButton:
  108.             theVoiceSpec = theDefaultVoiceSpec;
  109.             break;
  110.             
  111.          case kPlaySpeechButton:
  112.             theChannel = OpenOneSpeechChannelVoice( theVoiceSpec );
  113.             if ( theChannel == nil )
  114.                ExitToShell();               
  115.             theError = SpeakText( theChannel, (Ptr)(theString + 1), theString[0] );
  116.             while ( SpeechBusy() == true )
  117.                ;                    
  118.             theError = DisposeSpeechChannel( theChannel );
  119.             if ( theError != noErr )
  120.                ExitToShell();  
  121.             break;
  122.  
  123.          case kQuitButton:
  124.             allDone = true;
  125.             break;
  126.       }
  127.       
  128.       if ( theError == kNoMatchingVoiceErr )
  129.       {
  130.          SysBeep( 0 );
  131.          theVoiceSpec = theDefaultVoiceSpec;
  132.       }
  133.       else if ( theError != noErr )
  134.          ExitToShell();
  135.    }
  136.  
  137.    DisposeDialog( theDialog ); 
  138. }
  139.  
  140. //____________________________________________________________
  141.  
  142. OSErr  GetVoiceSpecBasedOnAgeGender( VoiceSpec *theVoiceSpec, 
  143.                                      short      theAgeLo, 
  144.                                      short      theAgeHi, 
  145.                                      short      theGender )
  146. {
  147.    OSErr             theError;
  148.    short             theNumVoices;
  149.    short             theIndex;
  150.    VoiceDescription  theVoiceDesc;
  151.    
  152.    theError = CountVoices( &theNumVoices );
  153.    if ( theError != noErr )
  154.       return ( theError );
  155.  
  156.    for ( theIndex = 1; theIndex <= theNumVoices; theIndex++ )
  157.    {
  158.       theError = GetIndVoice( theIndex, theVoiceSpec );
  159.       if ( theError != noErr )
  160.          return ( theError );
  161.  
  162.       theError = GetVoiceDescription( theVoiceSpec, &theVoiceDesc, 
  163.                                       sizeof( theVoiceDesc ) );      
  164.       if ( theError != noErr )
  165.          return ( theError );
  166.       
  167.       if ( (theVoiceDesc.age >= theAgeLo) && (theVoiceDesc.age <= theAgeHi) )
  168.          if ( theVoiceDesc.gender == theGender )
  169.             return ( noErr );
  170.    }
  171.    return ( kNoMatchingVoiceErr );
  172. }
  173.  
  174.  
  175. //____________________________________________________________
  176.  
  177. SpeechChannel  OpenOneSpeechChannelVoice( VoiceSpec theVoiceSpec )
  178. {
  179.    SpeechChannel  theChannel;   
  180.    OSErr          theError;
  181.    
  182.    theError = NewSpeechChannel( &theVoiceSpec, &theChannel );
  183.    if ( theError != noErr )
  184.    {
  185.       theError = DisposeSpeechChannel( theChannel );
  186.       theChannel = nil;
  187.    }
  188.       
  189.    return ( theChannel );
  190. }
  191.  
  192.  
  193. //____________________________________________________________
  194.  
  195. Boolean  IsSpeechAvailable( void )
  196. {
  197.    OSErr    theError;
  198.    long     theResult;
  199.    Boolean  speechAvail;
  200.    
  201.    theError = Gestalt( gestaltSpeechAttr, &theResult );
  202.    if ( theError != noErr )
  203.       ExitToShell();
  204.       
  205.    speechAvail = theResult & ( 1 << gestaltSpeechMgrPresent );  
  206.    if ( speechAvail > 0 )
  207.       return ( true );
  208.    else
  209.       return ( false );
  210. }
  211.  
  212.  
  213. //____________________________________________________________
  214.  
  215. void  InitializeToolbox( void )
  216. {
  217.    InitGraf( &qd.thePort );
  218.    InitFonts();
  219.    InitWindows();
  220.    InitMenus();
  221.    TEInit();
  222.    InitDialogs( 0L );
  223.    FlushEvents( everyEvent, 0 );
  224.    InitCursor();
  225. }
  226.  
  227.  
  228.  
  229.